#include "client.h"
#include <cstdlib>

int main(int argc, char* argv[]) {
    if (argc < 4) {
        std::cerr << "Usage: " << argv[0] << " <server_ip> <port1,port2,...> <num_connections>" << std::endl;
        std::cerr << "Example: " << argv[0] << " 127.0.0.1 8080,8081,8082 10000" << std::endl;
        return 1;
    }

    // 解析端口列表
    std::vector<int> ports;
    std::string portStr = argv[2];
    size_t pos = 0;
    while ((pos = portStr.find(',')) != std::string::npos) {
        ports.push_back(atoi(portStr.substr(0, pos).c_str()));
        portStr.erase(0, pos + 1);
    }
    ports.push_back(atoi(portStr.c_str()));

    // 验证连接数
    int numConnections = atoi(argv[3]);
    if (numConnections <= 0 || numConnections > 100000) {
        std::cerr << "Invalid num_connections: " << argv[3] << " (must be 1~100000)" << std::endl;
        return 1;
    }
    std::cout << "=== Client will create " << numConnections << " connections ===" << std::endl;

    // 启动客户端
    EchoClient client(argv[1], ports, numConnections, 3);
    if (client.init()) {
        client.start();
    }
    return 0;
}